function RandomSwipe(qx, qy, zx, zy, time) {
    var xxy = [time];
    var point = [];
    var dx0 = {
        "x": qx,
        "y": qy
    };
    var dx1 = {
        "x": random(qx - (device.width / 4) * 0.25, qx + (device.width / 4) * 0.25),
        "y": random(qy, qy + 50)
    };
    var dx2 = {
        "x": random(zx - (device.width / 4) * 0.25, zx + (device.width / 4) * 0.25),
        "y": random(zy, zy + 50)
    };
    var dx3 = {
        "x": zx,
        "y": zy
    };
    for (var i = 0; i < 4; i++) {
        eval("point.push(dx" + i + ")");
    };
    
    
    for (let i = 0; i < 1; i += 0.01) {
        
        xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)]
        xxy.push(xxyy);

    }
    
    gesture.apply(null, xxy);
    
};

function bezier_curves(cp, t) {
    cx = 3.0 * (cp[1].x - cp[0].x);
    bx = 3.0 * (cp[2].x - cp[1].x) - cx;
    ax = cp[3].x - cp[0].x - cx - bx;
    cy = 3.0 * (cp[1].y - cp[0].y);
    by = 3.0 * (cp[2].y - cp[1].y) - cy;
    ay = cp[3].y - cp[0].y - cy - by;

    tSquared = t * t;
    tCubed = tSquared * t;
    result = {
        "x": 0,
        "y": 0
    };
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
    result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
    return result;
};


let funs = function () {

    this.funs = {
        click : function (x, y, range, fun) {
            print("run funs.click function")
            if (range != null && range != "" && range > 0) {
                x = x+random(-range, range)
                y = y+random(-range, range)
                
            } else {
                throw "参数不正确"
            }
            if (x < 1 || y < 1) {
                return 

            }
            press(x, y, random(50, 99))
            if (fun != null) {
                fun()
            }
        },

        swipe: function (qx, qy, zx, zy, time) {
            function bc(cp, t) {
                cx = 3.0 * (cp[1].x - cp[0].x);
                bx = 3.0 * (cp[2].x - cp[1].x) - cx;
                ax = cp[3].x - cp[0].x - cx - bx;
                cy = 3.0 * (cp[1].y - cp[0].y);
                by = 3.0 * (cp[2].y - cp[1].y) - cy;
                ay = cp[3].y - cp[0].y - cy - by;
            
                tSquared = t * t;
                tCubed = tSquared * t;
                result = {
                    "x": 0,
                    "y": 0
                };
                result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
                result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
                return result;
            };
            var xxy = [time];
            var point = [];
            var dx0 = {
                "x": qx,
                "y": qy
            };
            var dx1 = {
                "x": random(qx - (device.width / 4) * 0.25, qx + (device.width / 4) * 0.25),
                "y": random(qy, qy + 50)
            };
            var dx2 = {
                "x": random(zx - (device.width / 4) * 0.25, zx + (device.width / 4) * 0.25),
                "y": random(zy, zy + 50)
            };
            var dx3 = {
                "x": zx,
                "y": zy
            };
            for (var i = 0; i < 4; i++) {
                eval("point.push(dx" + i + ")");
            };
            
            for (let i = 0; i < 1; i += 0.01) {
                
                
                xxyy = [parseInt(bc(point, i).x), parseInt(bc(point, i).y)]
                xxy.push(xxyy);
            }
            gesture.apply(null, xxy);
        },
    
        isolny : function () {
            print("run funs.isolny function")
            
        },
    
        xBuddling : function () {
            print("run funs.xBuddling function")
    
        },
    
        left_swipe : function () {
            print("run funs.left_swipe function")
            
            RandomSwipe(random(device.height * 0.65, device.height * 0.8), random(device.width * 0.35, device.width * 0.75), random(device.height * 0.25, device.height * 0.4), random(device.width * 0.25, device.width * 0.85), random(800, 1600))
            
            

        },

        // 左滑动
        right_swipe : function () {
            RandomSwipe(random(400, 766), random(630, 750), random(150, 450), random(1500, 1700), random(800, 1600))
            
        },

        // 左右滑动
        lr_swipe : function () {
            if (random(0,1) == 1) {
                RandomSwipe(random(1500, 1700), random(630, 750), random(150, 450), random(400, 766), random(800, 1600))
            } else {
                RandomSwipe(random(400, 766), random(630, 750), random(150, 450), random(1500, 1700), random(800, 1600))
            }
        },
        
    }

}

module.exports = funs